package huawei;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 树状结构查询
 */
public class Q117 {

    static Map<Character, Set<Character>> treeMap = new TreeMap<>();

    static List<Character> list = new ArrayList<>();

    public static void main(String[] args) {
        String[] strings = {"b a", "c a", "d c", "e c", "f d"};
        Character c = 'c';

        for (String s : strings) {
            List<Character> collect = Arrays.stream(s.split(" ")).map(String::trim).map(s1 -> s1.charAt(0)).collect(Collectors.toList());
            Character k = collect.get(1);
            Character v = collect.get(0);
            if (treeMap.containsKey(k)) {
                treeMap.get(k).add(v);
            } else {
                Set<Character> set = new HashSet<>();
                set.add(v);
                treeMap.put(k, set);
            }
        }

        System.out.println(treeMap);

        dfs(c);

        list.sort(Comparator.comparingInt(o -> o));

        System.out.println(list);
    }

    private static void dfs(Character c) {
        if (treeMap.containsKey(c)) {
            Set<Character> characters = treeMap.get(c);
            for (Character character : characters) {
                list.add(character);
                dfs(character);
            }
        }
    }
}
